home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / development kits / hypercard related / xcmds & xfcns / byrne's xcmds&xfcns / source / itemoffset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-06  |  4.7 KB  |  192 lines

  1. /*
  2.  
  3.     ItemOffset XFCN v1.2
  4.     
  5.     ©1990 Apple Computer, Inc.; by Mike Byrne
  6.     
  7.     ItemOffset won't initially make sense, but it has its uses.  Suppose you have a comma-separated list
  8.     of things (items to a HyperCard list), and you want to know where in the list the string "daffy"
  9.     occurs.  This will tell you what item number contains "daffy".  The optional third parameter can
  10.     be used to specify a delimiter other than a comma.
  11.     
  12.     Form:
  13.     ItemOffset(<target>, <container>, [<delimiter>])
  14.  
  15.     # the MPW 3.2 build commands:
  16.     C -b ItemOffset.c -mbg off
  17.         Link -w -t STAK -c WILD -rt XFCN=608 ∂
  18.             -m ENTRYPOINT ∂
  19.             -sg ItemOffset ∂
  20.             ItemOffset.c.o ∂
  21.             "{Libraries}HyperXLib.o" ∂
  22.             "{Libraries}Runtime.o" ∂
  23.             "{Libraries}Interface.o" ∂
  24.             "{CLibraries}StdCLib.o" ∂
  25.             -o "teststack"
  26. */
  27.  
  28. #include <Types.h>
  29. #include <string.h>
  30. #include <Packages.h>
  31. #include <Memory.h>
  32. #include <CType.h>
  33. #include "HyperXCmd.h"
  34.  
  35. #define NULL (long) 0
  36. #define NIL (long) 0
  37.  
  38. /* prototypes */
  39. void ErrorBack(XCmdPtr paramPtr, char *message);
  40. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  41. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  42.  
  43.  
  44. pascal void EntryPoint(XCmdPtr paramPtr)
  45. {
  46.     /* declarations */
  47.     char        delim;
  48.     char        target[100];
  49.     char        block[100];
  50.     Str255        returnVal;
  51.     short        i = 0;
  52.     short        j = 0;
  53.     short        k = 0;
  54.     short         length;
  55.     short         stopLength;
  56.     short         location = 0;
  57.     Boolean        done = false;
  58.     
  59.  
  60.  
  61.     /* move high and lock the parameters. */
  62.     MoveLockParams(paramPtr, paramPtr->paramCount);
  63.  
  64.     /* check for copyright or syntax help request */
  65.     if (!strcmp( (char*)*paramPtr->params[0], "!") && (paramPtr->paramCount == 1) ) {
  66.         ErrorBack(paramPtr, "v1.2, ©1990-1 Apple Computer, Inc.; by Mike Byrne");
  67.         UnlockParams(paramPtr, paramPtr->paramCount);
  68.         return;
  69.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") && (paramPtr->paramCount == 1) ) {
  70.         ErrorBack(paramPtr, "ItemOffset syntax is 'ItemOffset(<target>, <container>, [<delimiter>])'");
  71.         UnlockParams(paramPtr, paramPtr->paramCount);
  72.         return;
  73.     }
  74.  
  75.     /* not a copyright or help request.       */     
  76.     /* check for correct number of parameters */
  77.     if ( (paramPtr->paramCount < 2) || (paramPtr->paramCount > 3) ) {
  78.         ErrorBack(paramPtr, "Error: ItemOffset syntax is 'ItemOffset(<target>, <container>, [<delimiter>])'");
  79.         UnlockParams(paramPtr, paramPtr->paramCount);
  80.         return;
  81.     }
  82.  
  83.  
  84.     /* set up the delimiter */
  85.     if (paramPtr->paramCount == 2) {
  86.         delim = ',';
  87.     } else {
  88.         switch ( (*(paramPtr->params[2]))[0] ) {
  89.             case 'r': case 'R':  
  90.                 { delim = '\n'; break;  }                                // a return
  91.             case 't': case 'T':  
  92.                 { delim = '\t'; break;  }                                // a tab
  93.             case 's': case 'S':
  94.                 { delim = ' '; break; }                                     // a space
  95.             default:  
  96.                 { delim = (*(paramPtr->params[2]))[0];  break;  }
  97.         }
  98.     }
  99.  
  100.     /* create the target string and convert to uppercase.  */
  101.     strcpy(target, (char*) *(paramPtr->params[0]) );
  102.     length = strlen(target);
  103.     for (i=0; i < length; i++) {
  104.         target[i] = toupper(target[i]);
  105.     }
  106.  
  107.     /* the actual searching part. */
  108.     do {
  109.         /* set up the searchStop */
  110.         stopLength = strlen( (char*) *(paramPtr->params[1]) );
  111.         
  112.         /* check to see if we're at the end. */
  113.         if (j>=stopLength) {
  114.             done = true;
  115.             location = 0;
  116.         } else {
  117.             /*  get the block  */
  118.             for (i=0; ( ((*(paramPtr->params[1]))[j] != delim) && (i < 100) && (j < stopLength) ); i++, j++) 
  119.                 { block[i] = (*(paramPtr->params[1]))[j]; }
  120.             block[i] = '\0'; 
  121.             j++;
  122.             
  123.             /* convert the block to uppercase */
  124.             length = strlen(block);
  125.             for (i=0; i < length; i++) {
  126.                 block[i] = toupper(block[i]);
  127.             }
  128.  
  129.             /* check and increment */
  130.             if (strstr(block, target))  { done = true; }
  131.             location++;
  132.         }
  133.     } while (!done);
  134.     
  135.     
  136.     /* convert the number to a string and go home.  */
  137.     NumToString( (long) location, &returnVal);
  138.     p2cstr(returnVal);
  139.     ErrorBack(paramPtr, returnVal);
  140.     return;
  141.     
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148.     
  149. /* allocate and load up paramPtr->returnValue with a string 
  150.    -------------------------------------------------------- */
  151. void ErrorBack(XCmdPtr paramPtr, char *message)
  152. {
  153.     Handle  mesHnd;
  154.  
  155.     /*
  156.         Allocate space for an error message.
  157.         Copy the string into it.
  158.         Return the handle to HyperCard.
  159.     */
  160.     mesHnd = NewHandle((long)(strlen(message)+1));
  161.     if (mesHnd == nil) return;
  162.     strcpy((char *)*mesHnd,message);
  163.     paramPtr->returnValue = mesHnd;
  164. }
  165.  
  166.  
  167.  
  168. /*  move high and lock down all parameters  
  169.     ----------------------------------------------------------------------- */
  170. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  171. {
  172.     short i;
  173.     
  174.     for(i=0; i <= paramCount-1; i++)
  175.     {
  176.         MoveHHi(paramPtr->params[i]);
  177.         HLock(paramPtr->params[i]);
  178.     }
  179. }
  180.  
  181.  
  182.  
  183.  
  184. /* unlock all parameter handles in the XCmdBlock  
  185.    ---------------------------------------------  */
  186. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  187. {    short i;
  188.     
  189.     for(i=0; i <= paramCount-1; i++)
  190.         { HUnlock(paramPtr->params[i]);}
  191. }
  192.